import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)
const Login = () => import('../pages/Login')
const Layout = () => import('../pages/Layout')
const Index = () => import('../pages/menu/Index')
const Routers = [
  {
    path:"/login",
    name:"login",
    component:Login,
    meta:{
      title:"登录"
    }
  },
  {
    path:'/',
    name:"layout",
    component:Layout,
    children:[
        {
          path:'/menu',
          name:"menu",
          component:Index,
          meta:{
            title:"菜单管理"
          }
        }
    ],
    meta:{
      title:"首页"
    }
  
  }
]
const router = new Router({
  routes : Routers
})

router.beforeEach(
  function(to,from,next){
      // console.log(to.path)
      if(to.path != "/login"){
        const userData = JSON.parse(localStorage.getItem("userData"))||{};
        console.log(userData.token);
          if(!userData.token){
            return next("/login")
          }
      }
      document.title = to.meta.title
      next()
  
     
  }
)

export default router